<%@ page language="java" import="java.util.*,cn.ac.cintcm.dataconverge.util.Constants,com.hs.core.utils.*" pageEncoding="UTF-8"%>
<%@ page import ="cn.ac.cintcm.dataconverge.researchsubject.domain.*,com.hs.core.page.PageUtil,com.hs.core.utils.StringUtilTool" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Subject obj = (Subject)request.getAttribute("subjectObj");
String imagePath = path + "/index/";
Map depMap = (Map)request.getAttribute("depMap");
Map orgMap = (Map)request.getAttribute("orgMap");
Subject subject = (Subject)request.getAttribute("subject");
Long  currentOrgId = -1l;
if(request.getAttribute("currentOrgId") != null)
  currentOrgId = (Long)request.getAttribute("currentOrgId");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title><%=Constants.APP_TITLE %></title>  
	<script language="javascript" type="text/javascript" src="<%=path %>/js/My97DatePicker/WdatePicker.js"></script>
	<script language="javascript" src="<%=path %>/index/Scripts/script.js"></script>
	<script language="javascript" src="<%=path %>/js/common.js"></script>
	<link href="<%=path %>/index/Themes/com/style.css" type="text/css" rel="stylesheet"></link>
	<script src='<%=basePath%>dwr/engine.js'></script>
     <script src='<%=basePath%>dwr/util.js'></script> 
     <script src='<%=basePath%>dwr/interface/subject.js'></script> 
	
	<script type="text/javascript">
	 var xmlHttp;
      var url="";
      var delNode;
      function createXMLHttpRequest() {
			if (xmlHttp == null) {
	    		if (window.ActiveXObject) {
	    			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    		}
	    		else if (window.XMLHttpRequest) {
	        		xmlHttp = new XMLHttpRequest();                
	    		}
    		}
	  }
			  
      function changeOrg(){
      		var orgId=document.form0.org.value;
            url="subject.htm?action=getDepartments&orgId="+orgId;
     	    createXMLHttpRequest();
			xmlHttp.open("GET", url, true);
    		xmlHttp.onreadystatechange = callback;
    		xmlHttp.send(null);
      }
      
      function callback(){
      	if(xmlHttp.readyState==4){
    			if(xmlHttp.status==200){
    				clearResults();
    				parseMessage();
    			}
    		}
      }
      
      function parseMessage(){
      		var xmlDoc = xmlHttp.responseXML;
			var nodes = xmlDoc.getElementsByTagName('department');
			if(nodes!=null){
				var id="";
				var name="";
				var res=document.getElementById("departments");
				var input="";
				var text="";
				for(var i=0;i<nodes.length;i++){
					id=nodes[i].firstChild.firstChild.nodeValue;
					name=nodes[i].lastChild.firstChild.nodeValue;
					//alert("id is "+id+" name is "+name);
					input=document.createElement("input");
					input.type="checkbox";
					input.value=id;
					input.name="department";
					res.appendChild(input);
					
					text = document.createTextNode(name);   
					res.appendChild(text); 
				}
			}
			
      }
      
      function clearResults(){
         	var res=document.getElementById("departments");
         	while(res.childNodes.length > 0) {
				res.removeChild(res.childNodes[0]);
			}
      }
      var flag = true;
	  function check(){ 
	    flag = true;
        var org = document.getElementById("org").value;
       // var outOrg = document.getElementById("outOrgName").value; 
        var role = document.getElementById("role").value; 
        
       /** if(isNull(org)&&isNull(outOrg)){
           alert("请在“单位中选择”或“在单位查询中输入要新增的单位名称”");
           flag = false;
           return;
        } */
        if(isNull(org)){
           alert("请选择要添加的单位");
           flag = false; 
        }  
        if("0" == role){
           alert("请选择单位角色");
            flag = false; 
        }
        //判断选中单位是否已经存在
        //if(isNull(org)){
       // 	isAlreadyAdd(org); 
       // }
        if(flag)
       	  addDepartments();
         
      }
      function isAlreadyAdd(orgId){
          subject.checkSubjectOrg(orgId,callbackForAdd)
      
      }
      function callbackForAdd(message){
         if("2" == message){
         	alert("该单位已经添加到该课题中,不能重复添加!")
         	flag = false;
         }
      
      }
      
        function addDepartments(){ 
	        var ids="";
	        var nodes=document.getElementById("departments").childNodes;
	      	for(i=0;i<nodes.length;i++){
				if(nodes[i].checked==true){
					ids=ids+nodes[i].value+";";
					i++;
				}
			}
			var orgId=document.form0.org.value;
			url="subject.htm?action=addOrgs&subjectId=<%=subject.getId()%>&departmentIds="+ids+"&orgId="+orgId+"&role="+escape(document.form0.role.value);
			createXMLHttpRequest();
			xmlHttp.open("GET", url, true);
	    	xmlHttp.onreadystatechange = callbackForAddOrg;
	    	xmlHttp.send(null);
      }
      
      function callbackForAddOrg(){
      	if(xmlHttp.readyState==4){
    			if(xmlHttp.status==200){
    				var xmlDoc = xmlHttp.responseXML;
					var nodes = xmlDoc.getElementsByTagName('orgAdded');
					var added=nodes[0].firstChild.nodeValue;					
    				if(added>0){
    					alert("已成功添加"+added+"个单位部门!");  
      					window.location.replace(document.location.href);
      				}
      				else{
      					var errMsg=xmlDoc.getElementsByTagName('errMsg')[0].firstChild.nodeValue;
      					alert(errMsg);
      				}
    			}
    	}
      }
      
      function delSubOrg(subOrgId){
          if(confirm("确认要删除该单位吗？"))
          subject.delSubjectOrg(subOrgId,callbackForDelSubOrg);
      }
      
      function callbackForDelSubOrg(data){
         alert(data);
         window.location.replace(window.location.href);
      } 
      function orgSearch(){
      	 var orgName = document.getElementById("orgName").value;  
      	 dwr.util.removeAllOptions("org");
         subject.getOrgListInfo(orgName,CallBackSearchOrg);  
      }
      function CallBackSearchOrg(dataStr){  
       if(''!= dataStr){  
          dwr.util.addOptions("org", eval(dataStr),'id','name');  
       }  
     }
	</script>
</head>
<body  class="rtm_body"  cellspacing="0" cellpadding="0"  >
    <table cellpadding="0" cellspacing="0" border="0" valign="top" width="100%">
        <tr>
            <td>
                <div class="content">
                    <table width="*" cellpadding="0" cellspacing="1" class="dg_borderstyle" id="Table4">
                        <tr>
                            <td class="dg_headerstyle" width="5%">
                               	序号</td>
                            <td class="dg_headerstyle" width="35%">
                                 单位名称</td>
                            <td class="dg_headerstyle" width="35%">
                                 部门名称</td>
                             
                            <td class="dg_headerstyle">
                                单位角色</td>
                            <td class="dg_headerstyle">
                                删除</td> 
                        </tr>
                        <% 
                        System.out.println(subject);
                        Set orgs = subject.getSubOrg();
                        
                        if(orgs != null && orgs.size() > 0){
	                        Iterator iterator = orgs.iterator(); 
	                        int i=1;
	                        while(iterator.hasNext()){
	                        	SubOrg subOrg = (SubOrg)iterator.next();
	                        	if(null == subOrg || null == subOrg.getOrg() )
	                        		continue;
	                        %>
	                        <tr class="dg_alternatingitemstyle">
	                            <td align="center">
	                                <%=i++ %></td>
	                            <td>
	                                <%=subOrg.getOrg().getName()%>
	                            </td>
	                            <td>
	                                <%=StringUtilTool.setObjNULLStr(depMap.get(subOrg.getDeptId())) %>
	                            </td>
	                            <td align="center">
	                                <%
	                                if(Constants.org_role_leader.equals(subOrg.getOrgRole()))
	                                	out.print("牵头单位");
	                                if(Constants.org_role_participant.equals(subOrg.getOrgRole()))
	                                	out.print("参与单位");
	                                
	                                %>
	                            </td> 
	                            <td align="center">
	                                <img src="<%=imagePath %>Themes/com/68design.net_registration1_(delete)_16x16.gif" onclick="javascript:delSubOrg('<%=subOrg.getId() %>');"
	                                    alt="删除" />
	                            </td> 
	                        </tr> 
                        <%}}else{ %>
                          <tr class="dg_alternatingitemstyle"> 
                            <td align="center" colspan=5>
                                <font color="red">没有找到记录</font>
                            </td> 
                        </tr> 
                        
                        <%} %>
                    </table>  
                </div>
            </td>
        </tr>
    </table>
 
 <table cellpadding="0" cellspacing="0" width="100%" height="100%" id="Table8">
 <FORM name="form0">
 <input type="hidden" name="currentOrgId" value="<%=currentOrgId %>"/>
        <tr>
            <td valign="top" height="100%">
                <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" id="Table11">
                    <tr>
                        <td valign="top" >
                            <!------------------ 标题栏 ------------------>
                            <!--- 标题 ---->
                            <table class="idt_tle_mg" cellspacing="0" cellpadding="0" width="100%">
                                <tr>
                                    <td class="idt_tle_i">
                                        <img id="imgForm6" src="<%=basePath%>index/Themes/com/idt_tle_o.gif"></td>
                                    <td class="idt_tle_f1">
                                        添加单位</td>
                                </tr>
                            </table>
                            <!--- 内容 --->
                            <div id="tdForm">
                                <table border="0" cellpadding="0" cellspacing="1" width="100%" class="idt_fim_mg">
                                    <tr>
                                        <td class="fim_l" align="center" width="15%">
                                           单位查询</td>
                                        <td class="fim_r" colspan="3" nowrap>
                                        <input   onpropertychange="orgSearch();" id="orgName" class="com_textbox" type="text"  style="width:50%"/>
                                         
                                         <font color=red><b>提示：请先输入要添加的单位名称的全称或者简称进行查询!</b></font> 
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="fim_l" width="15%" align="center">
                                           可选单位</td>
                                        <td class="fim_r"  colspan=3 width="70%">
                                        <select name="org" onchange="changeOrg();" size="13" style="width:50%"> 
				    					<%   
				    					     Iterator iterator02 = orgMap.keySet().iterator();
					    					 while(iterator02.hasNext()){
					    					 Long key = (Long)iterator02.next();
				    					 %> 
				    					 	<option value="<%=key %>" <%if(key == currentOrgId)out.print("selected"); %>><%=orgMap.get(key) %></option>
				    					 <%} %>
				    			    </select>
				    			         <font color=red>提示:请在右侧列表中选择要添加的单位</font></b>
                                        </td>
                                         
                                    </tr>
                                    <tr>
                                       <td class="fim_l" align="center" width="15%">
                                            单位角色</td>
                                        <td class="fim_r" colspan=3>
                                           <select name="role" id="role">
						    					<option value="0">--请选择单位角色--</option>
						    					<option value="<%=Constants.org_role_leader%>">牵头单位</option>
						    					<option value="<%=Constants.org_role_participant%>">合作单位</option>
						    				</select>
						    				<b><font color=red>提示:所选择单位在课题中的角色</font></b>
                                           </td>
                                    </tr>
                                    <tr>
                                        <td class="fim_l" align="center" width="15%">可选部门</td>
                                        <td class="fim_r" id="departments" colspan="3"   nowrap>
                                        </td> 
                                    </tr>
                                    
                                </table>
                                <table cellspacing="0" cellpadding="0" width="100%" class="idt_bottom_mg" border="0">
                                    <tr>
                                        <td align="center">
                                         <input type="button" value="添加到课题"     onclick="check();">
                                            <!-- <input  class="com_btn2" id="Button1" name="save" type="button"  value="上一步">
                                            &nbsp; &nbsp;
                                             <input  class="com_btn2" id="Button1" name="save" type="button" value="下一步">
                                             -->
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            </form>
                </table>
</body>
</html>
